package arkham.data;

import java.awt.Color;

import arkham.data.Globals.*;
import arkham.jaxb.encounter.CardType;
import arkham.util.AHLogger;
import arkham.util.XmlTools;

public class CommonItem extends InvestigatorCard
{
	private int hands;
	private int cost;
	private CommonItemSubtype subtype;

	public CommonItem(String name)
	{
		super(name);
	}

	@Override
	public InvestigatorCardType getType()
	{
		return InvestigatorCardType.COMMON;
	}

	@Override
	public boolean isItem()
	{
		return true;
	}

	/**
	 * Gets the subtype of this common item. The list of subtypes can be found in
	 * arkham.data.Globals.CommonItemSubtype
	 *
	 * @return the subtype of this common item
	 */
	public CommonItemSubtype getSubtype()
	{
		return subtype;
	}

	/**
	 * Returns true if this item is a weapon
	 * @return true if this item is a weapon
	 */
	public boolean isWeapon()
	{
	    return subtype.equals(CommonItemSubtype.PHYS_WEAPON) ||
	        subtype.equals(CommonItemSubtype.MAG_WEAPON);
	}

	/**
	 * Gets the cost of this common item
	 * @return The cost of this item
	 */
	public int getCost()
	{
		return cost;
	}

	/**
	 * Gets the number of hands this item uses, if it is a weapon
	 * @return The number of hands the weapon uses
	 */
	public int getHands()
	{
		return hands;
	}

	public Color getPreferredColor()
	{
		return new Color(185, 185, 66);
	}

	/**
	 * This method creates a common item card from the appropriate XML info
	 * @param xmlcard The XML data
	 * @return The data representation of the object
	 */
	public static CommonItem createCardFromXml(CardType xmlcard)
	{
		CommonItem citem = new CommonItem(xmlcard.getName());
		citem.loadCommonFieldsFromXml(xmlcard);
		/* Get the cost of the card */
		int cost = -1;
		try
		{
			cost = XmlTools.getIntPropertyValue(xmlcard, "Cost");
		}
		catch (NumberFormatException e)
		{
			AHLogger.error(e.getMessage());
			return null;
		}
		if (cost < 1)
		{
			AHLogger.error(citem.getName() + " has an invalid cost " + cost);
			return null;
		}
		citem.cost = cost;

		/* Get the subtype */
		if (xmlcard.getSubtype() != null)
		{
			if (CommonItemSubtype.isValidCardType(xmlcard.getSubtype()))
			{
				citem.subtype = CommonItemSubtype.getSubType(xmlcard.getSubtype());
			}
			else
			{
				AHLogger.error(xmlcard.getName() + " has an invalid subtype " + xmlcard.getSubtype());
				return null;
			}
		}
		else
		{
			citem.subtype = CommonItemSubtype.OTHER;
		}

		/* Get the number of hands for weapons */
		if (citem.subtype.equals(CommonItemSubtype.PHYS_WEAPON) ||
                citem.subtype.equals(CommonItemSubtype.MAG_WEAPON))
		{
			int hands = -1;
			try
			{
				hands = XmlTools.getIntPropertyValue(xmlcard, "Hands");
			}
			catch (NumberFormatException e)
			{
				AHLogger.error(e.getMessage());
				return null;
			}
			if (hands < 0 || hands > 2)
			{
				AHLogger.error(citem.getName() + " has an invalid number of hands: " + hands);
				return null;
			}
			citem.hands = hands;
		}

		citem.setImage(xmlcard.getImg());

		return citem;
	}
}
